package com.yafeed.trade.pojo.dto.stock;

import cn.hutool.core.map.MapUtil;
import com.yafeed.trade.common.utils.IdGenerator;
import com.yafeed.trade.entity.industrystock.IndustryStock;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Map;

/**
 * @description: 行业股票信息
 * @author: yanghj
 * @time: 2020-12-13 17:40
 */
@Setter
@Getter
@ToString
public class IndustryStockDto implements Serializable {
    private static final long serialVersionUID = 7843101798043205633L;

    /**
     * 主键
     */
    private Long industryStockId;

    /**
     * 股票代码
     */
    private String stockCode;

    /**
     * 股票名称
     */
    private String stockName;

    /**
     * 当前价格
     */
    private BigDecimal currentPrice;

    /**
     * 昨天价格
     */
    private BigDecimal prePrice;

    /**
     * 涨跌额
     */
    private BigDecimal changeAmount;

    /**
     * 涨跌幅
     */
    private BigDecimal changeRange;


    /**
     * 成交量
     */
    private Long tradeVolume;

    /**
     * 成交额
     */

    private Long tradePrice;

    /**
     * 最高价
     */
    private BigDecimal highPrice;

    /**
     * 最低价
     */
    private BigDecimal lowPrice;

    private static IndustryStockDto of() {
        IndustryStockDto industryStockDto = new IndustryStockDto();
        industryStockDto.setIndustryStockId(IdGenerator.getGuid());
        return industryStockDto;
    }

    public static IndustryStockDto of(Map<String, Object> map) {
        if (map.isEmpty()) {
            throw new RuntimeException("map 不能为空");
        }
        IndustryStockDto industryStockDto = of();
        industryStockDto.setStockCode(MapUtil.getStr(map, "symbol"));
        industryStockDto.setStockName(MapUtil.getStr(map, "name"));
        industryStockDto.setCurrentPrice(MapUtil.get(map, "trade", BigDecimal.class));
        industryStockDto.setChangeAmount(MapUtil.get(map, "pricechange", BigDecimal.class));
        industryStockDto.setChangeRange(MapUtil.get(map, "changepercent", BigDecimal.class));
        industryStockDto.setPrePrice(MapUtil.get(map, "settlement", BigDecimal.class));
        industryStockDto.setTradePrice(MapUtil.get(map, "volume", Long.class));
        industryStockDto.setTradeVolume(MapUtil.get(map, "amount", Long.class));
        industryStockDto.setHighPrice(MapUtil.get(map, "high", BigDecimal.class));
        industryStockDto.setLowPrice(MapUtil.get(map, "low", BigDecimal.class));

        return industryStockDto;
    }

    public IndustryStock toIndustryStock() {
        IndustryStock industryStock = new IndustryStock();
        BeanUtils.copyProperties(this, industryStock);
        return industryStock;
    }

}
